package com.kuyun.base.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuyun.base.base.BaseController;
import com.kuyun.base.base.Page;
import com.kuyun.base.exception.KunYunException;
import com.kuyun.base.model.BaseRole;
import com.kuyun.base.model.BaseUser;
import com.kuyun.base.model.HttpResult;
import com.kuyun.base.model.ValidFormResult;
import com.kuyun.base.util.Constants;

/**
 * 角色管理控制器
 * @author 史梁辰
 * @date 2016-6-19下午10:40:26
 */
@Controller
@RequestMapping("/webback/BaseRole")
public class BaseRoleController extends BaseController {
	/**
	 * 跳转角色编辑页面 根据id判断是新增还是修改
	 * @author 史梁辰
	 * @date 2016-6-19下午10:50:50
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions({ "ROLE_ADD", "ROLE_UPDATE" })
	@RequestMapping("/toRoleEdit.do")
	public String toRoleEdit(String id, Model model) {
		if (!StringUtils.isEmpty(id)) {// 如果id不为空则为跳转修改页面
			try {
				model.addAttribute("role", baseRoleService.getRoleById(id));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "role/role_edit";
	}
	/**
	 * 跳转角色管理页面
	 * @author 史梁辰
	 * @date 2016-6-19下午10:50:50
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("BASEROLE_LIST")
	@RequestMapping("/toRoleList.do")
	public String toRoleList() {
		return "role/role_list";
	}
	/**
	 * 获取角色列表
	 * @author 史梁辰
	 * @date 2016-7-11下午5:54:12
	 * @param request
	 * @param page
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getRoleList.do")
	@RequiresPermissions("BASEROLE_LIST")
	public Object getUserList(HttpServletRequest request, Page<BaseRole> page, BaseRole role) {
		Page<BaseRole> p = new Page<BaseRole>();
		try {
			p.setRows(baseRoleService.findRole(page, role));
			p.setTotal(baseRoleService.getRoleCount(role));
			if (p.getTotal() == 0) {
				return new Page<BaseRole>();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}
	/**
	 * 新增或修改操作
	 * @author 史梁辰
	 * @date 2016-6-19下午11:13:27
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addOrUpdate.do")
	@RequiresPermissions({ "ROLE_ADD", "ROLE_UPDATE" })
	public ValidFormResult addOrUpdate(BaseRole role) {
		ValidFormResult result = new ValidFormResult();
		try {
			if (baseRoleService.addOrUpdate(role)) {
				result.setStatus(ValidFormResult.Y);
			} else {
				result.setStatus(ValidFormResult.N);
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof KunYunException) {
				result.setInfo(e.getMessage());
			}
		}
		return result;
	}
	/**
	 * 删除操作
	 * @author 史梁辰
	 * @date 2016-6-19下午11:13:47
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	@RequiresPermissions("ROLE_DELETE")
	public HttpResult delete(String id) {
		try {
			return baseRoleService.deleteRole(id);
		} catch (Exception e) {
			HttpResult result = new HttpResult();
			result.setDesc("服务器繁忙，请稍后再试");
			result.setResultCode(Constants.RESULT_FINAL);
			e.printStackTrace();
			return result;
		}
	}
	/**
	 * 检查角色名重复
	 * @author 史梁辰
	 * @date 2016-7-11下午5:27:59
	 * @param param
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/checkRoleName.do")
	public ValidFormResult checkUserName(String param, String id) {
		ValidFormResult result = new ValidFormResult();
		if (baseRoleService.checkRoleName(param, id) > 0) {
			result.setStatus(ValidFormResult.N);
			result.setInfo("角色名已存在");
		} else {
			result.setStatus(ValidFormResult.Y);
		}
		return result;
	}
	/**
	 * 获取角色树
	 * @author 史梁辰
	 * @date 2016-7-13下午8:50:08
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getRoleTree.do")
	public HttpResult getRoleTree(String id) {
		HttpResult result = new HttpResult();
		try {
			result.setObj(baseRoleService.getRoleTree(id));
			result.setResultCode(Constants.RESULT_SUCCESS);
		} catch (Exception e) {
			result.setResultCode(Constants.RESULT_FINAL);
			result.setDesc("服务器繁忙，请稍后重试");
		}
		return result;
	}
	/**
	 * 保存角色
	 * @author 史梁辰
	 * @date 2016-7-13下午8:50:08
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveUserRole.do")
	public HttpResult saveUserRole(String ids, String id) {
		HttpResult result = new HttpResult();
		try {
			baseRoleService.saveUserRole(ids, id);
			result.setResultCode(Constants.RESULT_SUCCESS);
			result.setDesc("保存成功");
		} catch (Exception e) {
			result.setResultCode(Constants.RESULT_FINAL);
			result.setDesc("服务器繁忙，请稍后重试");
		}
		return result;
	}
	/**
	 * 获取角色树
	 * @author 史梁辰
	 * @date 2016-7-13下午8:50:08
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getPriTree.do")
	public HttpResult getPriTree(String id) {
		HttpResult result = new HttpResult();
		try {
			result.setObj(baseRoleService.getPriTree(id));
			result.setResultCode(Constants.RESULT_SUCCESS);
		} catch (Exception e) {
			result.setResultCode(Constants.RESULT_FINAL);
			result.setDesc("服务器繁忙，请稍后重试");
		}
		return result;
	}
	/**
	 * 保存角色权限
	 * @author 史梁辰
	 * @date 2016-7-13下午8:50:08
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveRolePri.do")
	public HttpResult saveRolePri(String ids, String id) {
		HttpResult result = new HttpResult();
		try {
			baseRoleService.saveRolePri(ids, id);
			result.setResultCode(Constants.RESULT_SUCCESS);
			result.setDesc("保存成功");
		} catch (Exception e) {
			result.setResultCode(Constants.RESULT_FINAL);
			result.setDesc("服务器繁忙，请稍后重试");
		}
		return result;
	}
}
